<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29 0029
 * Time: 上午 9:12
 */

namespace app\admin\model;


use app\admin\model\Base;
use think\Db;


class Test extends Base
{

    protected $table = "authority_menu";
    protected $base_arr = [
            'table' => 'admin',
            'field' => 'admin_name,is_deleted,roleid',
            'primary_key' => 'admin_id'
            ];

    protected $attach_arr =  [
            [
            'table' => 'authority_role',
            'field' => 'rolename',
            'primary_key' => 'id',
            'base_table_field' => 'roleid'
            ],

            ];


    //预计一维数组,要查找的手机号
    protected $senMsgAttach = [

        'table' => 'patient',
        'field' => 'phone',
        'primary_key' => 'id',
        'base_table_field' => 'patient_id'

    ];


    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => 'authority_menu',//必填
            'model' => '菜单',//必填
            'menuname' => '功能名称',
            'upmenuid' => '上级菜单',
            'menuurl' => '链接地址',
            'state' => '{"0":"功能禁用","1":"功能启用","2":"功能删除"}'
        ];
        return $data;
    }


    /**
     * 获取日志的访问方法
     **/
    public static function addModelAdminLog($list_action='',$form_data=[],$userId,$admin_type){
        $model = new Model();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';
        switch($actionName){
            //列表
            case "list":
                Base::save_admin_log($userId, $model, 'check','',[],[],$admin_type);
                break;
            case "check_detail":
                $type = 'detail';
                $record_data['id'] = $form_data['id'];
                $form_data = DB::name($model->table)
                    ->field($model->base_arr['field'])
                    ->where($record_data)->find();
                $form_data['id'] = $record_data['id'];
                break;
            case "add":
                $type = 'add';
                if ($model->request->isPost()) {

                }
                break;
            case "edit":

                $type = 'edit';
                if ($model->request->isPost()) {



                    $old_data = Base::getOneOldDataByTables($form_data['id'],$model->base_arr,$model->attach_arr);

                }
                break;
            case "del":
                $type = 'del';
                if ($model->request->isPost()) {
                    Db::execute("update doctor set is_deleted=3 where id=?", [$form_data['id']]);
                }
                break;
        }


        if($model->request->isPost() ){

            //$model 实例化之后的模型
            //$type 数据的操作类型
            //$form_data  form表单提交之后的表单
            //$url 跳转路径
            //$old_data 原有的数据
            //$attach_arr 附属表单数组
            //$primary_key 基本表单的主键值id
            $result =  Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);
            return $result;
        }else if($type == 'detail'){
            $result =  Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type,false);
            return $result;
        }



        return true;


    }

    /**
    *如果需要发送短信
    **/
    public static function sendMsgToPatient($record_id){
        //获取当前数据操作问题
        $model = new Patientcase();
        $sendMsgName = Base::getOneFieldByTablesToSendMsg($record_id,$model->base_arr,$model->senMsgAttach);
        // print_r($sendMsgName);
        return $sendMsgName;
    }







}
